﻿#include"OrderManagementView.h"
#include"XQTablePagingWidget.h"
#include"XQAlgorithm.h"
#include<QBoxLayout>
#include<QPushButton>
OrderManagementView::OrderManagementView(QWidget* parent) :XQWidget(parent)
{
}

OrderManagementView::~OrderManagementView()
{
}

XQTablePagingWidget* OrderManagementView::tableView() const
{
	return m_tableView;
}

OrderModel OrderManagementView::order(qint64 index)
{
	if (m_tableView->dataSize() <= index || index < 0)
		return OrderModel();
	auto& item = *m_tableView->datas()[index];
	OrderModel model;
	int nSel = 0;
	model.setOrder_id(item.data(nSel++).toULongLong());
	model.setAccount(item.text(nSel++));
	model.setProduct_id(item.data(nSel++).toULongLong());
	model.setName(item.text(nSel++));
	model.setType(item.text(nSel++));
	model.setPrice(item.data(nSel++).toDouble());
	model.setNumber(item.data(nSel++).toULongLong());
	nSel++;
	model.setMTime(item.data(nSel++).toDateTime());
	model.setCTime(item.data(nSel++).toDateTime());
	return model;
}

void OrderManagementView::setOrder(qint64 index, const OrderModel& model)
{
	if (m_tableView->dataSize() <= index || index < 0)
		return;
	auto& item = *m_tableView->datas()[index];
	item.clear();
	item.push_back(model.order_id());
	item.push_back(model.account());
	item.push_back(model.product_id());
	item.push_back(model.name());
	item.push_back(model.type());
	item.push_back(QString::number(model.price(), 'f', 2) + "元", model.price());
	item.push_back(model.number());
	item.push_back(QString::number(model.totalPrice(), 'f', 2) + "元", model.totalPrice());
	item.push_back(model.mtime().toString("yyyy-MM-dd hh:mm:ss"), model.mtime());
	item.push_back(model.ctime().toString("yyyy-MM-dd hh:mm:ss"), model.ctime());
}

void OrderManagementView::setOrder(const QList<OrderModel>& list)
{
	clear();
	for (auto& data : list)
		addOrder(data);
}

void OrderManagementView::addOrder(const OrderModel& model)
{
	XQTablePagingItem item;
	item.push_back(model.order_id());
	item.push_back(model.account());
	item.push_back(model.product_id());
	item.push_back(model.name());
	item.push_back(model.type());
	item.push_back(QString::number(model.price(), 'f', 2) + "元", model.price());
	item.push_back(model.number());
	item.push_back(QString::number(model.totalPrice(), 'f', 2) + "元", model.totalPrice());
	item.push_back(model.mtime().toString("yyyy-MM-dd hh:mm:ss"), model.mtime());
	item.push_back(model.ctime().toString("yyyy-MM-dd hh:mm:ss"), model.ctime());
	m_tableView->push_backRow(item);
}

void OrderManagementView::removeOrder(const OrderModel& model)
{
	m_tableView->removeShowData_if([=](const XQTablePagingItem& item) {
		if (item[0].data.toULongLong() == model.product_id())
			return true;
		});
	m_tableView->updataTable();
}

void OrderManagementView::clear()
{
	m_tableView->clearData();
	m_tableView->clearTable();
	m_tableView->showTable();
}

void OrderManagementView::showOrder(int page)
{
	m_tableView->showTable(page);
}

void OrderManagementView::updataTable()
{
	m_tableView->updataTable();
}

void OrderManagementView::init()
{
	XQWidget::init();
	init_table();
}

void OrderManagementView::init_ui()
{
	XQWidget::init_ui();
	m_tableView = new XQTablePagingWidget(this);
	auto layout = new QBoxLayout(QBoxLayout::TopToBottom, widget());//从上到下
	/*layout->addWidget(init_operateButton());
	layout->addWidget(m_tableView);
	widget()->setLayout(layout);*/
	setWidget(m_tableView);
}

void OrderManagementView::init_table()
{
	m_tableView->setHorizontalTitle({
			"订单ID","用户账号","商品ID","商品名称","商品类型","商品价格","数量","商品总价","修改时间","创建时间"
		});
}
